import request from '@/axios';
import {baseUrl} from '@/config/env';


export const listApi = (urlName, obj) => request({
    url: baseUrl + urlName,
    method: 'post',
    data: obj
})
export const getListApi = (urlName) => request({
    url: baseUrl + urlName,
    method: 'get',
})
export const addApi = (urlName, obj) => request({
    url: baseUrl + urlName,
    method: 'post',
    data: obj
})
export const delApi = (urlName, id) => request({
    url: baseUrl + urlName + '/' + id,
    method: 'delete'
})
export const updateApi = (urlName, obj) => request({
    url: baseUrl + urlName,
    method: 'put',
    data: obj
})


export const serverlessApi = (serverless, param) => {
    let functionParams = [];
    functionParams.push(JSON.stringify(param))
    let functionRequest = {
        functionTarget: serverless.functionTarget,
        functionMethod: serverless.functionMethod,
        functionParams: functionParams
    }
    return request({
        url: baseUrl + serverless.url,
        method: 'post',
        data: functionRequest
    })
};

// 发送 http请求,requestType: get,post,json
export const httpRequest = (url, data, requestType = "get") => {
    if (!url.startsWith('http')) {
        url = baseUrl + url;
    }
    if (requestType === "get") {
        return request({
            url: url,
            method: 'get',
            params: data
        })
    } else if (requestType === "post") {
        return request({
            url: url,
            method: 'post',
            data: data
        })
    } else if (requestType === "json") {
        return request({
            url: url,
            method: 'post',
            data: json.stringify(data),
            headers: {}
        })
    }

}

